package com.heroCW.bodhiAttic.controller;

import com.heroCW.bodhiAttic.entity.ShoppingCart;
import com.heroCW.bodhiAttic.resultUtil.Result;
import com.heroCW.bodhiAttic.resultUtil.ResultCode;
import com.heroCW.bodhiAttic.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    @Transactional
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据：{}", shoppingCart);

        if (shoppingCart.getSetmealId() == null && shoppingCart.getDishId() == null) {
            return Result.error(ResultCode.CUSTOM_DATA_ERR,"数据异常");
        }

        // 查询当菜品或套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOneShoppingCart(shoppingCart);
        boolean flag;

        if (cartServiceOne != null) {
            // 如果已经存在，就在原来的数量基础上+1
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            flag = shoppingCartService.updateById(cartServiceOne);
        } else {
            // 如果不存在，则添加到购物车，数量默认为1
            shoppingCart.setNumber(1);
            // shoppingCart.setCreateTime(LocalDateTime.now());
            flag = shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        if (flag) {
            return Result.success(ResultCode.SAVE_OK, cartServiceOne);
        } else {
            return Result.error(ResultCode.SAVE_ERR, "商品添加失败");
        }
    }

    /**
     * 减少购物车
     *
     * @return
     */
    @PostMapping("/sub")
    @Transactional
    public Result<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据：{}", shoppingCart);

        if (shoppingCart.getSetmealId() == null && shoppingCart.getDishId() == null) {
            return Result.error(ResultCode.CUSTOM_DATA_ERR,"数据异常");
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOneShoppingCart(shoppingCart);
        Integer number = cartServiceOne.getNumber();
        cartServiceOne.setNumber(number - 1);
        boolean flag = false;

        if (number > 1) {
            flag = shoppingCartService.updateById(cartServiceOne);
        } else if (number == 1) {
            flag = shoppingCartService.removeById(cartServiceOne.getId());
        }

        if (flag) {
            return Result.success(ResultCode.DELETE_OK, cartServiceOne);
        } else {
            return Result.error(ResultCode.DELETE_ERR, "商品减少失败");
        }

    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        log.info("查看购物车");

        List<ShoppingCart> list = shoppingCartService.getList();
        if (list != null) {
            return Result.success(ResultCode.GET_OK, list);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到购物车数据");
        }
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> clean() {

        Boolean flag = shoppingCartService.clean();

        if (flag) {
            return Result.success(ResultCode.DELETE_OK, "购物车清空成功");
        } else {
            return Result.error(ResultCode.DELETE_ERR, "购物车清空失败");
        }
    }


}
